package com.sense.kukoo.common.wiget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Checkable;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;

import com.sense.kukoo.common.R;
import com.sense.kukoo.common.utils.ApplicationContext;
import com.sense.kukoo.common.wiget.dialog.BottomConfigDialog;
import com.sense.kukoo.lib_picture.loader.Monet;

public class MySubscriptionsItemView extends FrameLayout {

    //private CardView card_view;
    private ImageView iv_music_icon;
    private ImageView iv_red_dot;
    private ImageView btn_subscribe;
    private TextView tv_music_des;
    private TextView tv_music_msg;

    private boolean isSubscribed;
    private OnSubscriptionsChangedListener mListener;
    private View root;

    public MySubscriptionsItemView(@NonNull Context context) {
        super(context);
        initView();
    }

    public MySubscriptionsItemView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public MySubscriptionsItemView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public MySubscriptionsItemView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView();
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.common_my_subscriptions_item, this, true);
        //
        root = findViewById(R.id.root);
        //card_view = findViewById(R.id.card_view);
        iv_music_icon = findViewById(R.id.iv_music_icon);
        iv_red_dot = findViewById(R.id.iv_red_dot);
        btn_subscribe = findViewById(R.id.iv_btn_subscribe);
        tv_music_des = findViewById(R.id.tv_music_collection);
        tv_music_msg = findViewById(R.id.tv_music_msg);
        //
        root.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mListener != null){
                    mListener.onItemClick(MySubscriptionsItemView.this);
                }
            }
        });
        //
        btn_subscribe.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isSubscribed) {
                    BottomConfigDialog dialog = new BottomConfigDialog(getContext());
                    BottomConfigDialog.BottomConfigListener listener = new BottomConfigDialog.BottomConfigListener() {
                        @Override
                        public void onButtonDownClick() {
                            dialog.dismiss();
                        }

                        @Override
                        public void onButtonUpClick() {
                            onToggle();
                            dialog.dismiss();
                        }
                    };
                    dialog.setBtn_1(com.sense.kukoo.common_i18n.R.string.my_subscriptions_cancel, getResources().getColor(R.color.dialog_btn_text_color_blue))
                            .setBtn_2(com.sense.kukoo.common_i18n.R.string.my_subscriptions_unsubscribe, getResources().getColor(R.color.dialog_btn_text_color_red))
                            .setHintMsg(com.sense.kukoo.common_i18n.R.string.my_subscriptions_unsubscribe_hint_msg)
                            .setListener(listener)
                            .show();

                } else {
                    onToggle();
                }
            }
        });
    }

    public void setChecked(boolean checked) {
        isSubscribed = checked;
        if (isSubscribed) {
            btn_subscribe.setImageResource(R.drawable.common_icon2_sub_gray_28dp);
        } else {
            btn_subscribe.setImageResource(R.drawable.common_icon2_sub_red_28dp);
        }
    }

    public boolean isChecked() {
        return isSubscribed;
    }

    private void onToggle(){
        boolean newChecked = !isSubscribed;
        if(mListener != null){
            mListener.onChanged(newChecked);
        }
    }

    public void isShowRedDot(boolean isShow){
        iv_red_dot.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    public void setIconUrl(String url){
        Monet.get(getContext())
                .load(url)
                .placeholder(R.mipmap.common_icon_default_editor_picks)
                .into(iv_music_icon);
    }

    public void setDes(String s){
        tv_music_des.setText(s);
    }

    public void setMsg(String s){
        tv_music_msg.setText(s);
    }

    public interface OnSubscriptionsChangedListener{
        void onChanged(boolean isChecked);
        void onItemClick(View v);
    }

    public void setListener(OnSubscriptionsChangedListener l){
        mListener = l;
    }
}
